package com.hvgroup.modules.sanic.service.factory;


import com.fasterxml.jackson.databind.JsonNode;
import com.hvgroup.modules.sanic.domain.enums.ChartTypeEnum;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class TableChartHandler implements ChartHandler {

    @Override
    public ChartTypeEnum getChartType() {
        return ChartTypeEnum.TABLE_CHART;
    }

    @Override
    public List<Map<String, Object>> handle(JsonNode llmInfo, JsonNode columnData, JsonNode chartData) {
        List<Map<String, Object>> result = new ArrayList<>();
        if (chartData.isArray()) {
            for (JsonNode row : chartData) {
                Map<String, Object> item = new HashMap<>();
                Iterator<Map.Entry<String, JsonNode>> fields = row.fields();
                while (fields.hasNext()) {
                    Map.Entry<String, JsonNode> entry = fields.next();
                    item.put(entry.getKey(), entry.getValue().asText());
                }
                result.add(item);
            }
        }
        return result;
    }
}
